package com.neulogistics.core.customercenter.infrastructure.persistence.repository;

import com.neulogistics.core.customercenter.domain.model.aggregate.Customer;
import com.neulogistics.core.customercenter.domain.repository.ICustomerRepository;
import com.neulogistics.core.customercenter.infrastructure.persistence.converter.CustomerEntityPOConverter;
import com.neulogistics.core.customercenter.infrastructure.persistence.orm.po.CustomerPO;
import com.neulogistics.core.customercenter.infrastructure.persistence.orm.service.ICustomerPOService;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

@Repository
public class CustomerRepository implements ICustomerRepository {

    @Resource
    ICustomerPOService customerPOService;

    @Override
    public void save(Customer customer) {
        CustomerPO customerPO = CustomerEntityPOConverter.entityToPO(customer);
        customerPOService.saveOrUpdate(customerPO);
        // 在新增的情况下，需要返回生成的主键
        customer.setCustomerId(customerPO.getCustomerId());
    }

    @Override
    public Customer findById(Integer id) {
        CustomerPO customerPO = customerPOService.getById(id);
        Customer customer = CustomerEntityPOConverter.POToEntity(customerPO);
        return customer;
    }

    @Override
    public void deleteById(Integer id) {
        customerPOService.removeById(id);
    }
}
